﻿using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using OAuthUtility.Properties;
using System.IO;
using OAuth.Domain;
using System.Xml.Serialization;

namespace OAuthUtility
{
    public partial class MainWindow
    {
        private ServiceProviderItem _currentServiceProvider;
        private WebProxy _proxy;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void ChkProxyChecked(object sender, RoutedEventArgs e)
        {
            gProxy.IsEnabled = chkProxy.IsChecked.HasValue && chkProxy.IsChecked.Value;
        }


        private void TokensTabGotFocus(object sender, RoutedEventArgs e)
        {
            if (chkProxy.IsChecked.HasValue && chkProxy.IsChecked.Value)
            {
                _proxy = new WebProxy(txtUrlProxy.Text);

                if (!string.IsNullOrEmpty(txbUserProxy.Text))
                    _proxy.Credentials = new NetworkCredential(txtUserProxy.Text,
                                                              pwdPasswordProxy.Password,
                                                              txtDomainProxy.Text);

            }

            var img = (Image)comboServicesProvider.SelectedItem;
            imgServiceProviderSelected.Source = img.Source;
            imgServiceProviderSelected.Height = img.Height;
            imgServiceProviderSelected.Width = img.Width;

            LoadUrls(img.Name);
        }

        private void LoadUrls(string serviceProviderName)
        {
            var xml = File.ReadAllText("../../Configuration/ServiceProviders.xml");
            var xmlSerializer = new XmlSerializer(typeof(ServiceProviders));
            var stringReader = new StringReader(xml);
            var serviceProviders = (ServiceProviders)xmlSerializer.Deserialize(stringReader);

            _currentServiceProvider = (from s in serviceProviders.Services
                                       where s.Name == serviceProviderName
                                       select s).First();
        }


        private void BtnGetTokensClick(object sender, RoutedEventArgs e)
        {
            var oAuth = new OAuthTools.OAuth(txtConsumerKey.Text.Trim(), txtConsumerSecret.Text.Trim(), proxy: _proxy);

            var tokens = oAuth.GetRequestToken(_currentServiceProvider.RequestTokenUrl);

            new PIN(_currentServiceProvider, tokens.Value).ShowDialog();

            if (!string.IsNullOrEmpty(Settings.Default.PIN))
            {
                var accessTokens = oAuth.GetAccessToken(_currentServiceProvider.AccessTokenUrl, tokens.Value, tokens.SecretToken, Settings.Default.PIN.Trim());

                txtAccessToken.Text = accessTokens.Value;
                txtAccessSecret.Text = accessTokens.SecretToken;
            }
            else
                MessageBox.Show("We really need your account authorization!");
        }
    }
}
